public class MyLinkList implements LinkList{
    static class ListNode {
        //数据
        public int val;
        //节点的引用
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //用来存储表头地址
    public static ListNode Head;

    //创建一个简易链表
    public static void Greatlist(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        Head = node1;
    }
    //检查链表是否为空
    public static void IsEmpty(){
        if (Head == null){
            throw new EmptyEXception("链表为空");
        }
    }
    //检查index位置是否正确
    public static void IsIndex(int A){
        int count =0;
        ListNode cur = Head;

        while (cur != null){
            cur = cur.next;
            count++;
        }
        if (A > count || A < 0){
            throw new IndexException("Index位置异常");
        }
    }
    public static int Size(){
        int count = 0;
        ListNode cur = Head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    //头插法
    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);

        if (Head == null){
            Head = listNode;
            return;
        }

        listNode.next = Head;
        Head = listNode;
    }
    //尾插法
    @Override
    public void addLast(int data) {

        ListNode cur = Head;
        ListNode listNode = new ListNode(data);

        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = listNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
//        IsEmpty();
//        IsIndex(index);

        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == Size()-1){
            addLast(data);
            return;
        }

        ListNode listNode = new ListNode(data);

        ListNode cur = Head;
        ListNode pur = Head;

        for (int i = 1; i < index; i++) {
            cur = cur.next;
        }
        pur = cur.next;
        cur.next = listNode;
        listNode.next = pur;
    }
    //查看链表中是否包含key
    @Override
    public boolean contains(int key) {
        ListNode listNode = Head;
        while (listNode.next != null){
            listNode = listNode.next;
            if (listNode.val == key){
                return true;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = Head;
        ListNode pur = Head;
        int count = 0;
        while (cur.val != key){
            cur = cur.next;
            count++;
        }
        for (int i = 1; i < count; i++) {
            pur = pur.next;
        }
        pur.next = cur.next;
    }

    @Override
    public void removeAllKey(int key) {
        ListNode prve = Head;
        ListNode cur = Head.next;

        while (cur != null){
            if (cur.val != key){
                prve = cur;
                cur = cur.next;
            }else{
                prve.next = cur.next;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        return Size();
    }

    @Override
    public void clear() {
        Head = null;
    }

    @Override
    public void display() {
        ListNode cur = Head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
